package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Orders;
import com.example.demo.entity.Wallet;
import com.example.demo.entity.WalletRecord;
import com.example.demo.mapper.OrderMapper;
import com.example.demo.mapper.WalletMapper;
import com.example.demo.service.WalletRecordService;
import com.example.demo.mapper.WalletRecordMapper;
import com.example.demo.service.WalletService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDateTime;

/**
* @author 三石
* @description 针对表【wallet_record】的数据库操作Service实现
* @createDate 2024-06-13 09:00:04
*/
@Service
public class WalletRecordServiceImpl extends ServiceImpl<WalletRecordMapper, WalletRecord>
    implements WalletRecordService{

        @Autowired
        OrderMapper orderMapper;
        @Resource
        WalletMapper walletMapper;

        @Override
        public void insertWalletRecord(Long oid) {
            Orders order = orderMapper.selectById(oid);
            WalletRecord walletRecord=new WalletRecord();
            walletRecord.setOrderId(oid);
            walletRecord.setGoodsName(order.getGoodsName());
            walletRecord.setGoodsNumber(order.getCount());
            walletRecord.setGoodsPicture(order.getPicturePath());
            walletRecord.setType("支付");
            LocalDateTime dateTime = LocalDateTime.now();
            walletRecord.setCreateTime(dateTime);
            walletRecord.setUpdateTime(dateTime);
            walletRecord.setUserId(order.getUid());
            walletRecord.setMoney(order.getPayMoney());
            walletRecord.setOrderNum(order.getOrderNumber());

            //1.获取钱包余额
            Wallet wallet1=walletMapper.selectByUserId(order.getUid());
            UpdateWrapper<Wallet> wrapper = new UpdateWrapper<>();
            wrapper.eq("user_id", order.getUid());
            wrapper.set("total_money", money(wallet1.getTotalMoney()-order.getPayMoney()));

            walletRecord.setTotalMoney(money(wallet1.getTotalMoney()-order.getPayMoney()));
            //生成钱包数据
            baseMapper.insert(walletRecord);

            //2.更新钱包余额
            walletMapper.update(wrapper);

            Wallet wallet2=walletMapper.selectByUserId(order.getSid());
            UpdateWrapper<Wallet> wrapper2 = new UpdateWrapper<>();
            wrapper2.eq("user_id", order.getSid());
            wrapper2.set("total_money", money(wallet2.getTotalMoney()+order.getPayMoney()));

            walletMapper.update(wrapper2);
        }

    /**
     * 设置只保留两位小数
     * @param totalMoney
     * @return
     */
    public double money(double totalMoney){

            DecimalFormat df = new DecimalFormat("#.##");
            return Double.parseDouble(df.format(totalMoney));
        }
}




